package com.example.lin.day8_29;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show = findViewById(R.id.tv_show);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.m1:
                testAddSharedPreferences();
                break;
            case R.id.m2:
                testGetSP();
                break;
            case R.id.m3:
                testGetAllSP();
                break;
            case R.id.m4:
                testRemoveSp();
                break;
            case R.id.m5:
                testClearSP();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private static final String SP_FILE_NAME = "testSp";

    private void testClearSP() {
//获取 SharedPreferences 对象
SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);

SharedPreferences.Editor edit = sp.edit();
edit.clear();

boolean commit = edit.commit();//返回成功与否,仅当sd卡不存在才返回false
Toast.makeText(this, "commit:" + commit, Toast.LENGTH_SHORT).show();
    }

    private void testRemoveSp() {
//获取 SharedPreferences 对象
SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);

SharedPreferences.Editor edit = sp.edit();

edit.remove("isGirl");
edit.remove("name");

boolean commit = edit.commit();//返回成功与否,仅当sd卡不存在才返回false
Toast.makeText(this, "commit:" + commit, Toast.LENGTH_SHORT).show();

    }

    private void testGetAllSP() {

//获取 SharedPreferences 对象
        SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
        Map<String, ?> all = sp.getAll();//获取所有键值对

        StringBuilder sb = new StringBuilder();
        Set<? extends Map.Entry<String, ?>> entries = all.entrySet();
        for (Map.Entry<String, ?> entry : entries) {
            String key = entry.getKey();
            Object value = entry.getValue();
            sb.append(key).append(":").append(value).append("\n");
        }
        tv_show.setText(sb);
    }

    //测试 获取SharedPreferences 的数值
    private void testGetSP() {
//获取 SharedPreferences 对象
SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
//获取数值
boolean isGirl = sp.getBoolean("isGirl", false);
float money = sp.getFloat("money", 0);
int age = sp.getInt("age", 0);
long time = sp.getLong("time", 0);
String name = sp.getString("name", "");

        StringBuilder sb = new StringBuilder();

        sb.append(isGirl).append(",")
                .append(money).append(",")
                .append(age).append(",")
                .append(time).append(",")
                .append(name);
        tv_show.setText(sb);
    }

    private void testAddSharedPreferences() {
//获取 SharedPreferences 对象
        SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);

//获得 SharedPreferences 的 编辑对象 Editor
        SharedPreferences.Editor edit = sp.edit();

//使用 Editor 对象 存储/修改键值对(只支持存储的类型有:int,boolean,long,float,String)
        edit.putBoolean("isGirl", true);
        edit.putFloat("money", 7777f);
        edit.putInt("age", 18);
        edit.putLong("time", 1456789312456L);
        edit.putString("name", "莉莉");
//必须提交,否则 一切都白干
        boolean commit = edit.commit();//返回成功与否,仅当sd卡不存在或者存储空间不足才返回false

        Toast.makeText(this, "commit:" + commit, Toast.LENGTH_SHORT).show();
    }
}
